package com.example.test.controller;

import com.example.test.entity.Author;
import com.example.test.entity.VenueGoods;
import com.example.test.exception.DefinitionException;
import com.example.test.service.VenueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/venue")
public class VenueController {
    @Autowired
    private VenueService venueService;//属性注入
    @PostMapping("/list")
    //@RequestBody 接收post类型的json参数
    public List<Map<String,Object>> venueGoodsList(@RequestBody Map<String,Object> param){
        //System.out.println(param.get("goods_num").getClass().getSimpleName());//获取变量类型
        if(param.get("page") == "" ||param.get("page") == null ) throw new DefinitionException("page不能为空");
        Integer page = Integer.parseInt(param.get("page").toString());
        if(page<1) page = 1;
        Integer offset = page-1;
        param.put("offset",offset);
        param.put("page_size",10);
        List<Map<String,Object>> venueGoods = venueService.venueGoodsList(param);
        return venueGoods;
    }

    @PostMapping("/add")
    //RequestBody注解用于将HTTP POST请求正文部分的JSON或XML数据绑定到方法的参数上
    public String addVenueGoods(@RequestBody VenueGoods venueGoods){
        int num = venueService.addVenueGoods(venueGoods);
        if(num>0){
            return "ok";
        }
         throw new DefinitionException("添加失败");
    }

    @PostMapping("/del")
    //RequestBody注解用于将HTTP POST请求正文部分的JSON或XML数据绑定到方法的参数上
    public String delVenueGoods(@RequestBody VenueGoods venueGoods){
        Integer id = venueGoods.getId();
        if(id == null || id<0) throw new DefinitionException("id不能为空");
        int num = venueService.delVenueGoods(id);
        System.out.println(num);
        //log.info("测试输出日志");
        if(num>0){
            return "0k";
        }
        throw new DefinitionException("删除失败");
    }

//    @PostMapping("/info")
//    //@RequestParam接收查询参数，表单参数时，required默认为true 如果不传参数会出现Required request parameter .... is not present异常错误
//    public VenueGoods venueGoodsInfo(@RequestParam(name="venue_goods_id" ,required = false) Integer id) throws Exception{
//        if(id == null) throw new Exception("venue_goods_id不能为空");
//        VenueGoods venueGoods = venueService.venueGoodsInfo(id);
//        return venueGoods;
//    }

    @PostMapping("/info")
    //throw new Exception时需要手动在方法后throws Exception
    public VenueGoods venueGoodsInfo(@RequestParam(name="venue_goods_id" ,required = false) Integer id){
        if(id == null) throw new DefinitionException("venue_goods_id不能为空");
        VenueGoods venueGoods = venueService.venueGoodsInfo(id);
        return venueGoods;
    }

    @PostMapping("/author/list")
    public List<Map<String,Object>> authorList(){
        List<Map<String,Object>> author = venueService.authorList();
        return author;
    }



}
